<?php
/**
 * CreateTime: 2023/6/13 14:29
 */

namespace App\Http\Controllers\Staff;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Consts\Role\RoleConsts;
use App\Consts\TableConsts\TableConsts;
use App\Func\ResponseFunc;
use App\Func\RoleFunc;
use App\Func\ScopeFunc\AuthScopeStaffFunc;
use App\Http\Controllers\Controller;
use App\Http\Requests\ListBasicRequest;
use App\Http\Requests\Staff\AllocateStaffSeatRequest;
use App\Models\MeLawCase\MeLawCaseBoardModel;
use App\Models\Staff\StaffModel;
use App\Scopes\MediationStaffScope;
use App\Service\Staff\StaffDepartmentService;
use App\Service\Staff\StaffService;
use Illuminate\Support\Facades\DB;

class MeStaffController  extends Controller
{
    private StaffModel $staffModel;

    public function __construct()
    {
        $this->staffModel = new StaffModel();
    }

    public function getMeStaffSelect(){
        try {
            $query = $this->staffModel;
            $currentClientAuth = (new RoleFunc)->getStaffIdsByRoleCode(RoleConsts::ROLE_TYPE_TJY);

            $query = $query->select([
                "staff_id",
                "staff_id as key",
                "staff_name",
                "staff_login_account",
                "staff_phone_number",
            ])->whereIn('staff_id',$currentClientAuth)
                # 禁止全局 手动处理
                ->withoutGlobalScope(MediationStaffScope::class)
                ->whereIn('staff.staff_id',AuthScopeStaffFunc::getDepartmentStaffByCurrentStaffId())
            ->where('status',1);
            $data = $query->get()->toArray() ?? [];
            return ResponseFunc::returnJson(
                'ok',
                $data,
                '获取调解员列表成功',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '获取调解员失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
    }

    public function getExtensionsStaff(ListBasicRequest $request){
        try {
            $pFilters = $request->get('filters');
            $pageSize = $request->get('page_size');
            $filters = $pFilters ? json_decode($pFilters,true) : [];
            $query = $this->staffModel;
            $currentClientAuth = (new RoleFunc)->getStaffIdsByRoleCode(RoleConsts::ROLE_TYPE_TJY);

            $query = $query->select([
                "staff.staff_id",
                "staff.staff_id as key",
                "staff.staff_name",
                "staff.staff_login_account",
                "staff.staff_phone_number",
                "staff.staff_status",
                "staff.staff_status as staff_status_name",
                "staff.company_id",
                'staff.staff_last_login_time as staff_last_login_time_name',
                "staff.company_id as company_name_str",
                "staff.staff_seats_number",
            ])
            # 禁止全局 手动处理
            ->whereIn('staff.staff_id',$currentClientAuth)
            ->where('staff.status',1);

            if($filters){
                if(isset($filters['staff_status'])){
                    $query = $query->where('staff_status',$filters['staff_status']);
                }
                if(isset($filters['company_id'])){
                    $query = $query->where('company_id',$filters['company_id']);
                }
                if(isset($filters['staff_name']) && $filters['staff_name']){
                    $query = $query->where('staff_name','LIKE',"%".$filters['staff_name']."%");
                }
                if(isset($filters['staff_login_account']) && $filters['staff_login_account']){
                    $query = $query->where('staff_login_account','LIKE',"%".$filters['staff_login_account']."%");
                }
            }

            $data = $query->Paginate($pageSize)->toArray() ?? [];

            return ResponseFunc::returnJson(
                'ok',
                $data,
                '获取调解员列表成功',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '获取调解员列表失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
    }

    public function allocateStaffSeat($staffId,AllocateStaffSeatRequest $request){
        $data = $request->all();
        // 校验当前分机号和密码是否已经绑定过
        $isExists = $this->staffModel->where('staff_seats_number', $data['staff_seats_number'])
            ->where('staff_id','!=',$staffId)
            ->first();

        if($isExists){
            return ResponseFunc::returnJson(
                'error',
                null,
                '当前分机号已被绑定',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
        try {
            $res = $this->staffModel->where('staff_id',$staffId)->update([
                'staff_seats_pwd' => $data['staff_seats_pwd'],
                'staff_seats_number' => $data['staff_seats_number'],
            ]);
            return ResponseFunc::returnJson(
                'ok',
                null,
                '绑定分机号成功',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '绑定分机号失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
    }

    public function unAllocateStaffSeat($staffId){
        try {
            $res = $this->staffModel->where('staff_id',$staffId)->update([
                'staff_seats_pwd' => '',
                'staff_seats_number' => '',
            ]);
            return ResponseFunc::returnJson(
                'ok',
                [],
                '解绑分机号成功',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '解绑分机号失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
    }
}